package com.hack.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/validator-admin")
public class AdminController {
    @Autowired
    private RuleService ruleService;
    @PostMapping("/rules")
    public ResponseEntity<String> addRule(@RequestBody ValidationRule rule) {
        try {
            ruleService.addRule(rule);
            return ResponseEntity.ok("Rule added successfully");
        } catch (SQLException e) {
            return ResponseEntity.internalServerError().body("Failed to add rule: " + e.getMessage());
        }
    }

    @GetMapping("/rules")
    public ResponseEntity<?> getRules(@RequestParam String appId) {
        try {
            List<ValidationRule> rules = ruleService.getRules(appId);
            return ResponseEntity.ok(rules);
        } catch (SQLException e) {
            return ResponseEntity.internalServerError().body("Failed to get rules: " + e.getMessage());
        }
    }

    @DeleteMapping("/rules/{id}")
    public ResponseEntity<String> deleteRule(@PathVariable int id) {
        try {
            ruleService.deleteRule(id);
            return ResponseEntity.ok("Rule deleted successfully");
        } catch (SQLException e) {
            return ResponseEntity.internalServerError().body("Failed to delete rule: " + e.getMessage());
        }
    }
}